package com.cloudeasy.smtp.util;

import com.cloudeasy.api.vo.amqp.AuthCodeVO;
import com.cloudeasy.smtp.properties.SmtpProperties;
import org.springframework.mail.javamail.JavaMailSender;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SmtpUtil {
    /**
     * 构建验证码消息
     *
     * @param javaMailSender {@link JavaMailSender}
     * @param smtpProperties {@link SmtpProperties}
     * @param authCodeVO     {@link AuthCodeVO}
     * @return {@link MimeMessage}
     * @throws MessagingException 消息构建异常
     */
    public static MimeMessage buildAuthCode(JavaMailSender javaMailSender, SmtpProperties smtpProperties, AuthCodeVO authCodeVO) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        message.setFrom(smtpProperties.getMail().getSender());
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(authCodeVO.getRecipient()));
        message.setSubject("Topic：云逸社区验证");
        message.setText("您的验证码为：" + authCodeVO.getAuthCode());
        return message;
    }
}
